<?php
//print_r($_POST);
if(isset($_POST[txtSubsId]) && $_POST[txtSubsId] !='') 
{
	$sql = "SELECT * FROM tb_client_subscription,tb_deviceinfo,tb_clientinfo WHERE tcs_id =".$_POST[txtSubsId]." AND di_id = tcs_deviceId AND ci_id = tcs_clientId AND tcs_clientId =".$_POST[txtClientId];
	$rows = $db->query($sql);
	$clientRecord = $db->fetch_array($rows);
	//print_r($clientRecord);
    $endDate = date("d-m-Y",strtotime("-1 days ".($clientRecord[tcs_noOfMonths]) ."months ".$clientRecord[tcs_renewalDateFrom]));
}
else if(isset($_POST[txtClientId]) && $_POST[txtClientId] !='') 
{
	$sql = "SELECT *  FROM tb_deviceinfo,tb_clientinfo,tb_userinfo WHERE di_clientId=ui_clientId AND ui_isAdmin = 1 AND ui_clientId = ci_id AND ci_id=".$_POST[txtClientId];
	$rows = $db->query($sql);
	$clientRecord = $db->fetch_array($rows);
}
?>
<script>
$(function() {
	$( "#txtSubStartDate" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: "dd-mm-yy"
	});
	//$( "#txtSubStartDate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
});
$(function() {
	$( "#txtSubEndDate" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: "dd-mm-yy"
	});
	//$( "#txtSubEndDate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
});

function showHideSubsTable(val)
{
	if(val == "Cheque")
		document.getElementById('txtChequeNo').disabled = false;
	else
		document.getElementById('txtChequeNo').disabled = true;
}
Date.prototype.defaultView=function(){
var dd=this.getDate();
if(dd<10)dd='0'+dd;
var mm=this.getMonth()+1;
if(mm<10)mm='0'+mm;
var yyyy=this.getFullYear();
return String(dd+"-"+mm+"-"+yyyy)
}
function addMonth(d,month)
{
t = new Date (d);
var ed = new Date(new Date(t).setMonth(t.getMonth()+parseInt(month)));

return ed.defaultView();
} 
function showEndDate(sDate,durMonth)
{
	document.getElementById('txtEndDate').innerHTML = addMonth(sDate,durMonth);
}
</script>
<div class="pagearea"><!-- Pagearea div start here -->
<form name="frmClientSubs" id="frmClientSubs" action="?ch=Subscription" method="post">
<input type="hidden" name="txtClientId" id="txtClientId" value="<?php echo $_POST[txtClientId];?>"/>  
<input type="hidden" name="txtClientUserId" id="txtClientUserId" value="<?php echo $clientRecord[ui_id];?>"/>
<input type="hidden" name="txtDeviceId" id="txtDeviceId" value="<?php echo $_POST[txtDeviceId];?>" />
<input type="hidden" class="form_text" id="txtRemindDay" style="width:55%;" name="txtRemindDay" value="0" />
<?php
if(isset($_POST[txtSubsId]) && $_POST[txtSubsId] !=''){
?>
	<input type="hidden" name="txtSubsId" id="txtSubsId" value="<?php echo $_POST[txtSubsId];?>"/>  
<?php } ?>




<div class="form_area"><!-- form area div start here -->
       <ul class="form">
            <li>
            	<label>Client</label>
                <span style=" padding:5px 35px; text-align:left; float:left;"><?php echo ucfirst($clientRecord[ci_clientName]);?></span>
            </li>
            <li>
            	<label>Device</label>
                <span style=" padding:5px 35px; text-align:left; float:left;"><?php echo ucfirst($clientRecord[di_deviceId]);?></span>
            </li>
            <li>
            	<label>Subs. Start Date </label>
                <input type="text" class="form_text"  id="txtSubStartDate" name="txtSubStartDate" value="<?php if($clientRecord[tcs_renewalDateFrom]) echo date("d-m-Y",strtotime($clientRecord[tcs_renewalDateFrom])); else echo date("d-m-Y");?>" />
            </li>
            <li>
            	<label>Subs. End Date </label>
				<input type="text" class="form_text"  id="txtSubEndDate" name="txtSubEndDate" value="<?php if($clientRecord[tcs_renewalDateTo]) echo date("d-m-Y",strtotime($clientRecord[tcs_renewalDateTo])); else echo date("d-m-Y");?>" />
            </li>
            <!--<li>
            	<label>Remind me Before </label>
                <input type="text" class="form_text" id="txtRemindDay" style="width:55%;" name="txtRemindDay" value="<?php echo $clientRecord[tcs_reminderDays];?>" />&nbsp;Days
            </li>-->
            <li>
            	<label>Amount</label>
                <input type="text" class="form_text" id="txtSubsAmt" name="txtSubsAmt" value="<?php echo $clientRecord[tcs_amount];?>" />
            </li>
            <li>
            	<label>Payment Type</label>
                <select name="selPayType" class="form_select" style="width:63%;"  id="selPayType" onchange="showHideSubsTable(this.value)">
                  <option value="0">Select Payment Type</option>
                  <option <?php if($_POST[txtSubsId]!='') { if($clientRecord[tcs_payType] == "Cash") echo 'selected="selected"'; }?> value="Cash">Cash</option>
                  <option <?php if($_POST[txtSubsId]!='') { if($clientRecord[tcs_payType] == "Cheque") echo 'selected="selected"'; }?> value="Cheque">Cheque</option>
               </select>
            </li>
            <li>
            	<label>Cheque No</label>
               <?php  if($clientRecord[tcs_payType] == "Cheque") { ?>
                  <input type="text" class="form_text" id="txtChequeNo" name="txtChequeNo" value="<?php echo $clientRecord[tcs_chequeNo];?>" />
                  <?php } else { ?>
                  <input type="text" class="form_text" id="txtChequeNo" name="txtChequeNo" disabled="disabled" value="<?php echo $clientRecord[tcs_chequeNo];?>" />
                  <?php } ?>
            </li>
            
        </ul>     
         <p class="btn_area"> 
				<?php
				  if(isset($_POST[txtSubsId]) && $_POST[txtSubsId] !='')
					{
					?>
						<input type="submit" name="cmdSubmitUpdateSubs" id="cmdSubmitUpdateSubs" value="Update" class="save_btn" />  
				  <?php } else { ?>
						<input type="submit" name="cmdSubmitAddSubs" id="cmdSubmitAddSubs" value="Submit" class="save_btn" onclick="return getValid()" />
													 <input type="reset" value="Reset" class="save_btn" />
				  <?php } ?>
      	</p>   
        
</div>
        
</form>
 <script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("frmClientSubs");
 
  frmvalidator.addValidation("txtSubStartDate","req","Please Select Subs. Start Date");
  frmvalidator.addValidation("txtSubEndDate","req","Please Select Subs. End Date");
  
  frmvalidator.addValidation("txtRemindDay","maxlen=50");
  frmvalidator.addValidation("txtRemindDay","req","Please enter No. of Days to Remain");
  frmvalidator.addValidation("txtRemindDay","numeric","Please enter Days");
  
  frmvalidator.addValidation("txtSubsAmt","maxlen=50");
  frmvalidator.addValidation("txtSubsAmt","req","Please enter Amount");
  frmvalidator.addValidation("txtSubsAmt","numeric","Please enter Numbers");
  
  frmvalidator.addValidation("selPayType","dontselect=0","Please Select payment Type")
  
  function getValid()
  {
  	  if(document.frmClientSubs.selPayType.value == "Cheque")
	  {
		frmvalidator.addValidation("txtChequeNo","req","Please enter cheque no");
		frmvalidator.addValidation("txtChequeNo","numeric","Please enter Numbers");
		
	  }
	  return true;
  }
  
</script>   
</div><!-- PAGEarea END here -->
